<?php
namespace Lib\Info;
/**
 * Description of CopInfoAction
 *
 * @author chacha
 * @date 2016-5-20
 */
class Cop extends \BaseAction{
    //put your code here
    protected $copid = NULL;
    protected $copinfo = NULL;
    protected $tabcop = NULL;
    protected $tabcopinfo = NULL;

    public function __construct() {
        parent::__construct();
        $this->tabcop = M('user');
        $this->tabcopinfo = M('user_info');
    }

    protected function sidebar($copid) {
        $this->copinfo = $this->tabcopinfo->where('uid='.$copid)->find();
        $tabUC = M('usercomment');
        $rateArr = $tabUC->query("SELECT ROUND(AVG(rate1)) AS avg1,ROUND(AVG(rate2)) AS avg2,ROUND(AVG(rate3)) AS avg3,ROUND(AVG(rate4)) AS avg4 FROM xia_usercomment WHERE ct=1 and uid=".$copid);
        //好评率,大于7分算好评
        $total = $tabUC->where("ct=1 and uid=".$copid)->count();
        $w1 = $tabUC->where("ct=1 and rate1>=7 and uid=".$copid)->count();
        $w2 = $tabUC->where("ct=1 and rate2>=7 and uid=".$copid)->count();
        $w3 = $tabUC->where("ct=1 and rate3>=7 and uid=".$copid)->count();
        $w4 = $tabUC->where("ct=1 and rate4>=7 and uid=".$copid)->count();
        $wellrate = floor((($w1+$w2+$w3+$w4)/($total*4))*100);
        
        $this->assign('wellrate', $wellrate);
        $this->assign('copinfo', $this->copinfo);
        $this->assign('ratearr', $rateArr[0]);
    }
    
    public function info($copid) {
        $this->sidebar($copid);
        
        //企业信息
        $tabJob = M('job');
        $field="xia_job.*,team.name as teamname";
        $where = "team.uid=".$copid." and xia_job.state=1";
        $join1 = "LEFT JOIN xia_project prj ON prj.id=xia_job.pid";
        $join2 = "LEFT JOIN xia_team team ON team.id=prj.tid";
        $joblist = $tabJob->field($field)->join($join1)->join($join2)->where($where)->select();
        //评价详情
        $tabUC = M('usercomment');
        $field = "xia_usercomment.*, (xia_usercomment.rate1+xia_usercomment.rate2+xia_usercomment.rate3+xia_usercomment.rate4)/4 as rate,job.name as jobname,uif.nickname as employee";
        $join1 = "LEFT JOIN xia_user_info uif ON uif.uid=xia_usercomment.author";
        $join2 = "LEFT JOIN xia_job job ON job.id=xia_usercomment.job";
        $where = "ct=1 and status=1 and xia_usercomment.uid=".$copid;
        $count = $tabUC->where($where)->count();
        import('ORG.Util.Page');// 导入分页类
        $page = new \Page($count, 100);
        $nowPage = isset($_GET['p'])?$_GET['p']:1;
        $commentlist = $tabUC->field($field)->join($join1)->join($join2)->where($where)->page($nowPage.','.$page->listRows)->order("xia_usercomment.endate desc")->select();
//        dump($commentlist);
//        exit;
        
        $show       = $page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('joblist', $joblist);
        $this->assign('uclist', $commentlist);
        $tpl = APP_PATH."/Group/Bridge/Tpl/Cop_info.html";
        $this->display($tpl);
    }
}
